package com.wlw.eye.ui

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import com.gyf.barlibrary.ImmersionBar
import com.wlw.eye.R
import com.wlw.eye.adapter.WatchAdapter
import com.wlw.eye.mvp.model.bean.VideoBean
import com.wlw.eye.utils.ObjectSaveUtils
import com.wlw.eye.utils.SPUtils
import kotlinx.android.synthetic.main.activity_watch.*
import org.jetbrains.anko.async
import org.jetbrains.anko.uiThread
import kotlin.math.log

class WatchActivity : AppCompatActivity() {
    var mList = ArrayList<VideoBean>()
    lateinit var mAdapter: WatchAdapter
    lateinit var layoutManager: LinearLayoutManager
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImmersionBar.with(this).transparentBar().barAlpha(0.3f).fitsSystemWindows(true).init()
        setContentView(R.layout.activity_watch)
        setToolBar()
        layoutManager = LinearLayoutManager(this)
        recyclerView.layoutManager = layoutManager
        mAdapter = WatchAdapter(mList)
        recyclerView.adapter = mAdapter
        initData()
    }

    private fun initData() {
        async {
            val list = ArrayList<VideoBean>()
            val count = SPUtils.getInstance(this@WatchActivity, "beans").getInt("count")
            var i = count
            while (i > 0) {
                var videoBean = ObjectSaveUtils.getValue(this@WatchActivity, "bean$i") as VideoBean
                list.add(videoBean)
                i--
            }
            uiThread {
                mList.addAll(list)
                mAdapter.notifyDataSetChanged()
            }
        }


    }

    private fun setToolBar() {
        setSupportActionBar(toolbar)
        var bar = supportActionBar
        bar?.title = "观看记录"
        bar?.setDisplayHomeAsUpEnabled(true)
        toolbar.setNavigationOnClickListener {
            onBackPressed()
        }
    }
}
